《Android 基础(二十八)》 Fragment (1)

简介

Fragment,碎片,常用的内容,但是一直没有系统的学习下它的使用方法,花几天抽空看看随便记录一下。

生命周期

这里写图片描述
来自官网的图片一目了然。
自测试结果:
这里写图片描述

基本使用

自定义一个Fragment:BaseFragment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*带参数构造方法*/
public static BaseFragment newInstance(String content) {
Bundle args = new Bundle();
args.putString("CONTENT", content);
BaseFragment fragment = new BaseFragment();
fragment.setArguments(args);//传递参数,设置内容文本
return fragment;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.i(TAG, "onCreateView");
View view = inflater.inflate(R.layout.content_fragment
, container, false);
Bundle args = getArguments();//获取参数并设置文本内容
if (args != null) {
String content = args.getString("CONTENT");
tv = (TextView) view.findViewById(R.id.tv_content);
tv.setText(content);
}
return view;
}

对应布局文件

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:textColor="@color/colorAccent"
android:textSize="30sp"
android:textStyle="italic|bold" />

主Activity布局文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="jzfp.gs.com.animationdemo.MainActivity">

<!-- 工具栏-->
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"></android.support.v7.widget.Toolbar>
<!-- 内容区域-->
<FrameLayout
android:id="@+id/fl_content"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</LinearLayout>

Fragment切换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public class MainActivity extends AppCompatActivity {
BaseFragment leftFragment = null;//左侧fragment
BaseFragment rightFragment = null;//右侧Fragment
BaseFragment defaultFragment= null;//默认Fragment

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.left);
setSupportActionBar(toolbar);//设置ActionBar
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showLeftFragment();
}
});
defaultFragment = BaseFragment.newInstance("This is default fragment");
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.fl_content, defaultFragment);
transaction.commit();//显示默认碎片
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id){
case R.id.right:
showRightFragment();break;
default:break;
}
return true;
}

public void showLeftFragment(){
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
if(leftFragment == null) {
leftFragment = BaseFragment.newInstance("This is Left Fragment");
}
transaction.replace(R.id.fl_content, leftFragment);
transaction.commit();
}

public void showRightFragment(){
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
if(rightFragment == null) {
rightFragment = BaseFragment.newInstance("This is Right Fragment");
}
transaction.replace(R.id.fl_content, rightFragment);
transaction.commit();
}
}

结果

点击左右按钮切换fragment
这里写图片描述

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×